package action.creditapproval;

import java.sql.Timestamp;
import java.util.List;

import model.app.AppRate;
import model.credit.Creditapplyenterprise;
import model.credit.Creditapplypersonal;
import model.creditcheck.Applyopinion;
import model.creditcheck.Applystate;
import service.applycheck.ApplyOpinionService;
import service.applycheck.ApplyStateService;
import service.credit.CreditApplyEnterpriseService;
import service.credit.CreditApplyPersonalService;
import util.Log;
import util.Time;
import vo.CreditCheckBean;
import core.action.BaseAction;
import core.global.Global;
import exception.Exceptions;

public class CreditExamineAdviceAction extends BaseAction{
	private String state;
	private int type;
	private List<?> creditExamineAdviceBeanList;
	private CreditCheckBean creditCheckBean;
	private List<?> recordList;
	
	private AppRate appRate;
	
	private Integer applyid;
	private Integer appid;
	private int isCanChange;
	
	
	private String setopinion;
	
	private ApplyStateService applyStateService;
	private CreditApplyPersonalService creditApplyPersonalService;
	private CreditApplyEnterpriseService creditApplyEnterpriseService;
	private ApplyOpinionService applyOpinionService;
	
	private int searchwhat;
	
	private String searchApplyid;
	
	/**
	 * 显示等待。默认
	 * */
	public String execute() throws Exceptions{
		if(this.type == 1){
			this.creditExamineAdviceBeanList = this.creditApplyPersonalService.getAllExamineAdviceList("wait");
		}else if(this.type == 2){
			this.creditExamineAdviceBeanList = this.creditApplyEnterpriseService.getAllExamineAdviceList("wait");
		}
		this.searchwhat = 1;
		return "success";
	}
	/**
	 * 显示查询
	 * */
	public String searchList() throws Exceptions{
		try{
			this.applyid = Integer.parseInt(this.searchApplyid);
		}catch(Exception e){
			this.applyid = Integer.parseInt(this.searchApplyid.substring(3, this.searchApplyid.length()));
		}
		
		if(this.type == 1){
			this.creditExamineAdviceBeanList = this.creditApplyPersonalService.getAllAcceptionListByApplyIdAndState(this.applyid, "advice");
		}else if(this.type == 2){
			this.creditExamineAdviceBeanList = this.creditApplyEnterpriseService.getAllAcceptionListByApplyIdAndState(this.applyid, "advice");
		}
		return "success";
	}
	
	public String applyExamineAdvice() throws Exceptions{
		if (this.type == 1) {
			creditCheckBean = this.creditApplyPersonalService.getOneBean(applyid);
			recordList = this.applyOpinionService.getListAllByApplyId(applyid);
		} else if (this.type == 2) {
			creditCheckBean = this.creditApplyEnterpriseService.getOneBean(applyid);
			recordList = this.applyOpinionService.getListAllByApplyId(applyid);
		}
		return "detail";
	}
	/**
	 * 保存复查意见
	 * */
	public String doExamineAdvice() throws Exception{
		Timestamp nowTime = Time.CurrentTimestamp();
		int count = 0;
			if(this.type == 1){
				Creditapplypersonal _creditapplypersonal = this.creditApplyPersonalService.getOneById(appid);
				count = _creditapplypersonal.getCount();
				creditApplyPersonalService.save(_creditapplypersonal);
			}else if(this.type == 2){
				Creditapplyenterprise _creditapplyenterprise = this.creditApplyEnterpriseService.getOneById(appid);
				count = _creditapplyenterprise.getCount();
				creditApplyEnterpriseService.save(_creditapplyenterprise);
			}
			Applystate _applystate = null;
			if(applyStateService.getOneApplyStateByIdAndType(appid, "BB") != null){
				_applystate = applyStateService.getOneApplyStateByIdAndType(appid, "BB");
				_applystate.setCheckopinion(setopinion);
				_applystate.setCheckstate(1);
				_applystate.setChecktime(nowTime);
			}else{
				throw new Exceptions("信息为空!");
			}
		applyStateService.save(_applystate);
		this.applyOpinionService.save(new Applyopinion(appid, Global.userid().intValue(), null, "副审查意见", setopinion, null, nowTime, this.type, count));
		Log.save("插入", "填写审查意见，申请单号码为：" + appid, "操作人员为：" + Global.userid());
		MessageJump("审查意见填写完成", "creditExamineAdviceAction?type=" + this.type);
		return "message";
	}
	
	public String cdw() throws Exceptions{
		if(this.type == 1){
			this.creditExamineAdviceBeanList = this.creditApplyPersonalService.getAllExamineAdviceList("wait");
		}else if(this.type == 2){
			this.creditExamineAdviceBeanList = this.creditApplyEnterpriseService.getAllExamineAdviceList("wait");
		}
		this.searchwhat = 1;
		return "success";
	}
	public String cdd() throws Exceptions{
		if(this.type == 1){
			this.creditExamineAdviceBeanList = this.creditApplyPersonalService.getAllExamineAdviceList("done");
		}else if(this.type == 2){
			this.creditExamineAdviceBeanList = this.creditApplyEnterpriseService.getAllExamineAdviceList("done");
		}
		this.searchwhat = 2;
		return "success";
	}
	
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public List<?> getCreditExamineAdviceBeanList() {
		return creditExamineAdviceBeanList;
	}

	public void setCreditExamineAdviceBeanList(List<?> creditExamineAdviceBeanList) {
		this.creditExamineAdviceBeanList = creditExamineAdviceBeanList;
	}

	public CreditCheckBean getCreditCheckBean() {
		return creditCheckBean;
	}
	public void setCreditCheckBean(CreditCheckBean creditCheckBean) {
		this.creditCheckBean = creditCheckBean;
	}
	public List<?> getRecordList() {
		return recordList;
	}
	public void setRecordList(List<?> recordList) {
		this.recordList = recordList;
	}
	public AppRate getAppRate() {
		return appRate;
	}
	public void setAppRate(AppRate appRate) {
		this.appRate = appRate;
	}
	public Integer getApplyid() {
		return applyid;
	}
	public void setApplyid(Integer applyid) {
		this.applyid = applyid;
	}
	public Integer getAppid() {
		return appid;
	}
	public void setAppid(Integer appid) {
		this.appid = appid;
	}
	public int getIsCanChange() {
		return isCanChange;
	}
	public void setIsCanChange(int isCanChange) {
		this.isCanChange = isCanChange;
	}
	public String getSetopinion() {
		return setopinion;
	}
	public void setSetopinion(String setopinion) {
		this.setopinion = setopinion;
	}
	public ApplyStateService getApplyStateService() {
		return applyStateService;
	}
	public void setApplyStateService(ApplyStateService applyStateService) {
		this.applyStateService = applyStateService;
	}
	public ApplyOpinionService getApplyOpinionService() {
		return applyOpinionService;
	}
	public CreditApplyPersonalService getCreditApplyPersonalService() {
		return creditApplyPersonalService;
	}

	public void setCreditApplyPersonalService(
			CreditApplyPersonalService creditApplyPersonalService) {
		this.creditApplyPersonalService = creditApplyPersonalService;
	}

	public CreditApplyEnterpriseService getCreditApplyEnterpriseService() {
		return creditApplyEnterpriseService;
	}

	public void setCreditApplyEnterpriseService(
			CreditApplyEnterpriseService creditApplyEnterpriseService) {
		this.creditApplyEnterpriseService = creditApplyEnterpriseService;
	}

	public void setApplyOpinionService(ApplyOpinionService applyOpinionService) {
		this.applyOpinionService = applyOpinionService;
	}

	public int getSearchwhat() {
		return searchwhat;
	}

	public void setSearchwhat(int searchwhat) {
		this.searchwhat = searchwhat;
	}

	public String getSearchApplyid() {
		return searchApplyid;
	}

	public void setSearchApplyid(String searchApplyid) {
		this.searchApplyid = searchApplyid;
	}
	
	
	
}
